<?php

namespace app\admin\controller;

use app\admin\model\ArtAdopt;
use app\admin\model\Article as ModelArticle;
use app\admin\model\Category;
use app\admin\model\Tap;
use app\service\Tool;
use think\facade\Session;

class Article extends Base
{
    public function index()
    {
        $this->logs('[管理员]文章视图');
        $cate = Category::where('pid' , 0)->select();
        return view('index',['cate' => $cate]);
    }
    public function list()
    {
        $this->logs('[管理员]文章列表');
        $param = input();
        $param['type'] = array_key_exists('type', $param) ? $param['type'] : '';
        $list = ModelArticle::list($param);
        return success($list, '获取成功', 0);
    }
    public function getSecondCate()
    {
        $pid = input('pid');
        $list = Category::where('pid' , $pid)->select();
        return success($list);
    }
    public function update($id)
    {
        $this->logs('[管理员]更新文章视图');
        if (ModelArticle::findOrEmpty($id)->isEmpty()) return error('该ID文章不存在');
        $data = ModelArticle::find($id);
        if ($data->type == 3) {
            $adopt = ArtAdopt::where('article_id', $id)->find();
            if ($adopt) {
                $data['comType'] = $adopt->type;
            }
        }
        $categoryList = Category::list();
        $tapList = (new Tap())->list();
        return view('update', ['data' => $data, 'categoryList' => $categoryList, 'tapList' => $tapList]);
    }
    // 文章介绍图
    public function uploadImg()
    {
        $this->logs('[管理员]文章介绍图上传');
        $images = request()->file('img_path');
        return Tool::uploadImg($images , 'artilce');
    }
    public function add()
    {
        $categoryList = Category::list();
        $tapList = (new Tap())->list();
        return view('add', ['cateList' => $categoryList, 'tagList' => $tapList]);
    }
    public function doAdd()
    {
        $this->logs('添加文章');
        if (request()->isPost()) {
            $param = input();
            // 判断需要 ，判断是否为悬赏文章3 是添加时不需要改参数，更改时才需要
            if (array_key_exists('id', $param)) unset($param['id']);
            $param['user_id'] = Session::get('admin.user_id');
            $data = ModelArticle::handleParam($param);
            if (is_array($data)) {
                if (array_key_exists('comType', $data)) {
                    $comType = $data['comType'];
                    unset($data['comType']);
                }
                $data['status'] = 1;
                $res = ModelArticle::create($data);
                if ($res) {
                    if ($data['type'] == 3) {
                        ArtAdopt::create(['author_id' => Session::get('admin.user_id'), 'type' => $comType, 'article_id' => $res->id]);
                    }
                    return success();
                }
                return error('发布失败');
            }
            return error($data);
        }
    }
    // 审核全部文章
    public function examineAll()
    {
        $res = ModelArticle::where('status', 0)->update(['status' => 1 , 'update_uid' => $this->admin['user_id']]);
        if ($res) return success();
        return error();
    }
    /** 
     * @param int action 操作类型 1默认 更改文章全体内容 2 只处理一些字段
     * 
     */
    public function doUpdate($action = 1)
    {
        $this->logs('[管理员]更新文章内容');
        if (Request()->isPost()) {
            $param = input('post.');
            $art_id = $param['id'];
            if (!ModelArticle::findOrEmpty($art_id)->isEmpty()) {
                switch ($action) {
                    case 1:
                        $data = ModelArticle::handleParam($param);
                        $adopt = '';
                        if (is_array($data)) {
                            $data['update_uid'] = $this->admin['user_id'];
                            if ($data['type'] == 3) {
                                $comType = $data['comType'];
                                unset($data['comType']);
                                $adopt = ArtAdopt::where('article_id', $art_id)->update(['type' => $comType]);
                            }
                            $res = ModelArticle::update($data);
                            if ($res || @$adopt) return success();
                        } else return error($data);
                        break;
                    case 2:
                        // 这里不处理文章内容消息 ， 需要另外验证
                        // 处理审核 ，加精，置顶部分字段
                        if (array_key_exists('is_hot', $param) || array_key_exists('is_top', $param)) {
                            // 添加或取消置顶
                            $isTopOrHot = ModelArticle::find($param['id']);
                            if (array_key_exists('is_hot', $param)) {
                                $param['is_hot'] = $isTopOrHot->is_hot == 1 ? '0' : '1';
                            } else {
                                $param['is_top'] = $isTopOrHot->is_top == 1 ? '0' : '1';
                            }
                            $res = ModelArticle::update($param);
                            if ($res) return success();
                        }
                        if (array_key_exists('examine', $param)) {
                            $examId = explode(',', $param['id']);
                            $res = ModelArticle::whereIn('id', $examId)->update(['status' => 1,'update_uid' => $this->admin['user_id']]);
                            if ($res) return success();
                        }
                        break;
                }
                return error();
            }
        }
    }
    public function del()
    {
        $id = input('id');
        $delId = explode(',', $id);
        if (empty($id)) return error('文章id不能为空');
        $this->logs('[管理员]删除文章');
        for ($i = 0; $i < count($delId); $i++) {
            if (!empty($delId[$i])) {
                (new ModelArticle())->del($delId[$i]);
            }
        }
        return success();
    }
}
